package cn.ven.controller;

import cn.ven.entity.*;
import cn.ven.service.CartService;
import cn.ven.tool.RespJson;
import cn.ven.tool.Token;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/fore/cart")
public class CartController {
    private Logger logger =LoggerFactory.getLogger(CartController.class);

    @Autowired
    CartService cartService;

    //查找自己购物车所有的信息
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> findAll(){
        List<CartBook> cartBooks = cartService.findAll();
        return RespJson.pack(200,"",cartBooks);
    }

    //购物车转订单
    @RequestMapping("/addCart2Order")
    @ResponseBody
    public Map<String,Object> addCart2Order(@RequestBody OrdItem ordItem){
        if (Token.getUid()<0){  //uid<0,就是用户不存在
            return RespJson.pack(401,"未登录或者登录超时",null);
        }
        try {
            cartService.addCart2Order(ordItem);
        }catch (Exception e){
            logger.info("e===="+e);
            return RespJson.pack(500,"成功接收到数据, 但更新订单失败 ....",null);
        }
        return RespJson.pack(200,"购物车转订单成功",null);
    }



}
